import { _decorator, Component, Director, EventKeyboard, Input, input, instantiate, KeyCode, Label, Node, Prefab, Vec2 } from 'cc';
import { snake } from './snake';
import { food1 } from './food1';
const { ccclass, property } = _decorator;

export enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT,
}

//格子大小
export const SIZE = 74;
//网格数量
export const MAP_WIDTH = 20;
export const MAP_HEIGHT = 14;
//速度
const SPEED = 3;

@ccclass('snakeMgr')
export class snakeMgr extends Component {

    @property(Prefab) bodyPrefab: Prefab = null;
    @property(Node) food: Node = null;
    @property(snake) snakeHead: snake = null;
    @property(Node) snakeBody: Node = null;
    @property(Label) startLabel: Label = null;

    private foodPos: Vec2 = new Vec2(0, 0);
    private isPlaying: boolean = false;
    private isGameOver: boolean = false;
    private isStopped: boolean = false;
    protected onLoad(): void {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }
    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.snakeHead.Dir = Direction.LEFT;
                break;
            case KeyCode.KEY_D:
                this.snakeHead.Dir = Direction.RIGHT;
                break;
            case KeyCode.KEY_W:
                this.snakeHead.Dir = Direction.UP;
                break;
            case KeyCode.KEY_S:
                this.snakeHead.Dir = Direction.DOWN;
                break;
        }
    }

    start() {
        this.isPlaying = false;
    }

    updateGame() {
        const hPos = this.snakeHead.updateSnake();
        if (this.foodPos.equals(hPos)) {
            console.log("eat food!");
            this.foodPos = this.food.getComponent(food1).randomPos();
            this.snakeHead.createBody(this.bodyPrefab, this.snakeBody);
        }
        this.snakeHead.checkBody() && this.GameOver();
    }

    startGame() {
        if(!this.isPlaying || this.isGameOver) {
            this.isPlaying = true;
            this.snakeBody.removeAllChildren();
            this.snakeHead.nextBody = null;
            this.foodPos = this.food.getComponent(food1).randomPos();
            this.snakeHead.initSnake(new Vec2(SIZE / 2 + MAP_WIDTH / 2 * SIZE, SIZE / 2 + MAP_HEIGHT / 2 * SIZE), Direction.UP, true);
            this.schedule(this.updateGame, 1 / SPEED);
            this.startLabel.string = "重玩";
        }else{
            if(!this.isStopped) this.stopGame();
            this.snakeBody.removeAllChildren();
            this.snakeHead.nextBody = null;
            this.isPlaying = false;
            this.startGame();
        }
    }

    stopGame() {
        if(this.isPlaying && !this.isStopped){
            this.isStopped = true;
            this.unschedule(this.updateGame);
        }else{
            this.isStopped = false;
            this.schedule(this.updateGame, 1 / SPEED);
        }
    }

    GameOver() {
        console.log("Game Over!");
        this.isPlaying = false;
        this.isGameOver = true;
        this.startLabel.string = "开始";
        this.unschedule(this.updateGame);
    }

}


